package com.frame.demo.rest;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.frame.demo.util.PromptUtil;

/**
 * @author yuefeng
 * @version 3.3.1
 * @date 15/12/10
 * 作用：通用网络异常处理。非踢下线的情况也会显示Toast
 */
public class ExceptionDispose {
    public static final int SUCCESS = 200;
    public static final int EXCEPTION_TICK_OUT = 500;
    public static final int EXCEPTION_VALIDITY = EXCEPTION_TICK_OUT + 1;
    //网络异常
    public static final int EXCEPTION_NETWORK = EXCEPTION_VALIDITY + 1;
    //数据异常(脏数据)
    public static final int EXCEPTION_DIRTY_DATA = EXCEPTION_NETWORK + 1;
    //业务异常
    public static final int EXCEPTION_BUSINESS = EXCEPTION_DIRTY_DATA + 1;


    private Context mContext;


    public ExceptionDispose(Context pContext) {
        this.mContext = pContext;
    }

    public void disponse(RestError pRestError) {
        if (checkIsActivityInvalid()) return;
        String _msgStr = pRestError.getMsg();

        switch (pRestError.getErrorType()) {
            case EXCEPTION_TICK_OUT:
                popTickOutDialog(mContext, _msgStr);
                return;
            case EXCEPTION_VALIDITY:
                popShowOfficialTipDialog(mContext, _msgStr);
                return;
            default:
                //DDYCBizException之类的异常
                break;
        }
        PromptUtil.showNormalToast(_msgStr);
    }


//    public void disponse(Throwable pThrowable) {
//        String _msgStr;
//        if (checkIsActivityInvalid()) return;
//        _msgStr = pThrowable.getMessage();
//        if (pThrowable instanceof XKTickOutException) {
//            popTickOutDialog(mContext, _msgStr);
//            return;
//        }
//        if (pThrowable instanceof ValidityException) {
//            popShowOfficialTipDialog(mContext, _msgStr);
//        }
//        if (pThrowable instanceof DDYCBizException) {
//            DDYCBizException _exception = (DDYCBizException) pThrowable.getCause();
//            mCode = _exception.getCode();
//        }
//        PromptUtil.showNormalToast(_msgStr);
//
//    }

    private boolean checkIsActivityInvalid() {
        /** 发起请求的activity */
        Activity _activity = null;
        if (mContext == null) return true;
        if (mContext instanceof Activity) {
            _activity = (Activity) mContext;
        }
        /** 如果当前actity 处于不可用状态，截断回调 */
        if (_activity == null || _activity.isFinishing() || _activity.isRestricted()) return true;
        return false;
    }

    /**
     * 弹踢下线的弹框
     *
     * @param pContext
     * @param pMsg
     */
    private void popTickOutDialog(final Context pContext, String pMsg) {
//        PromptUtil.showNormalToast(pMsg);
//        LoginActivity.launch(pContext);
//        ActivityManager.getInstance().finishOther(LoginActivity.class.getSimpleName());
    }

    /**
     * 弹请下载官方版本弹框
     *
     * @param pContext
     * @param pMsg
     */
    private void popShowOfficialTipDialog(Context pContext, String pMsg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(pContext);
        builder.setTitle("提示");
        builder.setMessage(pMsg);
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }
}
